
#include "tMiseEnPalette.h"


void tMiseEnPalette()
{
	UINT32 nbPalettes = 0, nbRebuts;
	
	for ( ; ; )
	{
		MsgPalette * msgPalette;
		Lot * lotCourant;
		UINT32 numPaletteCourante, nbCartons, heureFin, tauxRebuts;
		char * buffer;
		char type;
		nbRebuts = 0;
		
		for (nbCartons = 0 ; nbCartons < nbCartonParPalette ; nbCartons += 1)
		{
			MsgCarton * msgCarton;
			
			amenerCarton();
			msgCarton = (MsgCarton *) malloc(sizeof(MsgCarton));
			msgQReceive(balCarton, (char *) msgCarton, sizeof(MsgCarton), WAIT_FOREVER);
			
			
			if (!testPalettePresente())
			{
				erreur(T_MISEENPALETTE, ERREUR_PALETTE);
			}
			
			if(msgCarton->num==MSG_FIN)
						{
							MsgPalette * msgPalette;
							msgPalette = (MsgPalette *) malloc(sizeof(MsgPalette));
							msgPalette->num = MSG_FIN;
							
							msgQSend(balPalette, (char *) msgPalette, sizeof(MsgPalette), WAIT_FOREVER, MSG_PRI_NORMAL);
							free(msgPalette);
							DBG("Fin de tMiseEnPalette");
							return;
						}
			nbPalettes += 1;			
			
			
			lotCourant = msgCarton->lot;
			numPaletteCourante = msgCarton->numPalette;
			nbRebuts += msgCarton->nbRebuts;
			
			DBG("Carton %d palette %d lot %d", msgCarton->num, nbPalettes, msgCarton->lot->num);
			
			free(msgCarton);
		}
		
		apposerFilmPlastique();
		
		msgPalette = (MsgPalette *) malloc(sizeof(MsgPalette));
		msgPalette->lot = lotCourant;
		msgPalette->num = numPaletteCourante; 
			
		lotCourant->palettesProduites ++;
		
		tauxRebuts = nbRebuts / nbCartons;
		
	
		/*
		 * 
		buffer = (char *) malloc(17 * sizeof(char));
		type = TRAME_VX_FIN_PALETTE;
		heureFin = time(NULL);
		memcpy(buffer, &type, 1);
		memcpy(buffer + 1, &numPaletteCourante, 4);
		memcpy(buffer + 5, &heureFin, 4);
		memcpy(buffer + 9, &tauxRebuts, 4);
		memcpy(buffer + 13, &(lotCourant->num), 4);
		msgQSend(balMessageLog, buffer, 17, WAIT_FOREVER, MSG_PRI_NORMAL);
		free(buffer);
		
		*/
		
		buffer = (char *) malloc(17 * sizeof(char));
				type = TRAME_VX_FIN_PALETTE;
				heureFin = time(NULL);
				memcpy(buffer, &type, 1);
				memcpy(buffer + 1, &numPaletteCourante, 4);
				memcpy(buffer + 5, &heureFin, 4);
				memcpy(buffer + 9, &tauxRebuts, 4);
				memcpy(buffer + 13, &(lotCourant->num), 4);
				msgQSend(balJournal, buffer, 17, WAIT_FOREVER, MSG_PRI_NORMAL);
				free(buffer);
		DBG("Palette %d lot %d ok", numPaletteCourante, lotCourant->num);
		
		
		if (nbPalettes >= lotCourant->quantite)
		{
			/*
			
			buffer = (char *) malloc(5 * sizeof(char));
			type = TRAME_VX_FIN_LOT;
			memcpy(buffer, &type, 1);
			memcpy(buffer + 1, &(lotCourant->num), 4);
			msgQSend(balMessageLog, buffer, 5, WAIT_FOREVER, MSG_PRI_NORMAL);
			free(buffer);
			
			*/
			
			buffer = (char *) malloc(5 * sizeof(char));
						type = TRAME_VX_FIN_LOT;
						memcpy(buffer, &type, 1);
						memcpy(buffer + 1, &(lotCourant->num), 4);
						msgQSend(balJournal, buffer, 5, WAIT_FOREVER, MSG_PRI_NORMAL);
						free(buffer);
			
			DBG("Lot %d ok", lotCourant->num);
					
			nbPalettes = 0;
		}
		
		
		msgQSend(balPalette, (char *) msgPalette, sizeof(MsgPalette), WAIT_FOREVER, MSG_PRI_NORMAL);
		free(msgPalette);
	}
}


